package com.aaa.yq127.controller;

import com.aaa.yq127.dao.impl.DeptDaoImpl;
import com.aaa.yq127.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet("/dept/*")
public class DeptServlet extends HttpServlet {
    DeptDaoImpl deptDao = new DeptDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        int i = uri.lastIndexOf("/");
        String s = uri.substring(i+1);
        switch (s){
            case "all":
                all(req,resp);
                break;
            case "delete":
                delete(req,resp);
                break;
            case "insert":
                insert(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
        }
    }
    protected void all(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        List<Map<String, Object>> all = deptDao.all();
        int i = all.size();
        Result result = new Result(i,all);
        String string = JSON.toJSONString(result);
        PrintWriter writer = resp.getWriter();
        writer.write(string);
        /*req.setAttribute("all",all);
        req.getRequestDispatcher("/dept.jsp").forward(req,resp);*/
    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String deptid = req.getParameter("deptid");
        int delete = deptDao.deleteDept(Integer.parseInt(deptid));
        req.getRequestDispatcher("/dept.jsp").forward(req,resp);
    }
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String deptNo = req.getParameter("deptNo");
        String dname = req.getParameter("dname");
        String remark = req.getParameter("remark");
        String status = req.getParameter("status");
        int i = deptDao.insertDept(Integer.parseInt(deptNo), dname, remark, Integer.parseInt(status));
        if (i>0){
            req.setAttribute("msg","添加成功");
        }else {
            req.setAttribute("msg","添加失败");
        }
        req.getRequestDispatcher("/adddept.jsp").forward(req,resp);
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String deptNo = req.getParameter("deptNo");
        String dname = req.getParameter("dname");
        String remark = req.getParameter("remark");
        String status = req.getParameter("status");
        String deptid = req.getParameter("deptid");
        int i = deptDao.updateDept(Integer.parseInt(deptNo), dname, remark, Integer.parseInt(status), Integer.parseInt(deptid));
        if (i>0){
            req.setAttribute("msg","修改成功");
        }else {
            req.setAttribute("msg","修改失败");
        }
        req.getRequestDispatcher("/updatedept.jsp").forward(req,resp);
    }
}
